<?php
/**
 * This file is part from Ewuroweb Shop
 *
 * @package 
 * @author Ivan I. Atanasov <i.atanasov@viscomp.bg>
 */


class EW_Core_Controller extends Zend_Controller_Action {

	/**
     * Instance of Redirector helper
     * @var Zend_Controller_Action_Helper_Redirector
     */
    protected $_redirector = null;

    /**
     * Replacement for init() method in Application controllers
     * @return void
     */
    protected function _initialize() {}

    /**
     *
     * @see Zend_Controller_Action::init()
     */
    final function init() {
        $this->_redirector = $this->_helper->getHelper('Redirector');
        
        if(FALSE) {
            $this->_initAuth();
        }
        
        $this->view->request = $this->_request;
        $this->_initialize();
    }
    //==========================================================================================
    
    /**
     * Internal Redirect
     * 
     * Redirect to page from this host only
     * 
     * @param string $goto	url relative (whithout http host name)
     */
    function redirectTo($goto) {
        $goto = empty($goto) ? array() : explode('/', $goto);
        
        $action = isset($goto[1]) ? $goto[1] : NULL;
        $controller = isset($goto[0]) ? $goto[0] : NULL;
        $module = NULL;
        $params = array();
        
        return $this->_helper->redirector($action, $controller, $module, $params);
    }
    //==========================================================================================
    
    private function _initAuth() {
        $auth = Zend_Auth::getInstance();
        $routeName = Zend_Controller_Front::getInstance()->getRouter()->getCurrentRouteName();
        
        if (!$auth->hasIdentity() and !($routeName == 'login' || $routeName == 'loginDefault')) {
            $this->_redirector->gotoRoute(array(), 'login');
        } else {
            if ($auth->hasIdentity()) {
                $this->view->user = $auth->getIdentity();
            }
        }
    }
}
